<?php require "inc/pageTop.php"; ?>

<style>
#noscript {
	display: none;
}

#purchaseFormError {
	background: #ffcc00;
	background-repeat: repeat-x;
	background-image: -moz-linear-gradient(top, #ffcc00, #E6B800);
	background-image: -ms-linear-gradient(top, #ffcc00, #E6B800);
	background-image: -webkit-gradient(linear, left top, left bottom, from(#ffcc00), to(#E6B800));
	background-image: -webkit-linear-gradient(top, #ffcc00, #E6B800);
	background-image: -o-linear-gradient(top, #ffcc00, #E6B800);
	background-image: linear-gradient(top, #ffcc00, #E6B800);
	text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.25);
	border: 1px solid rgba(0, 0, 0, 0.2);
	margin-bottom: 18px;
	margin-top: 15px;
	padding: 7px 14px;
	color: #404040;
	text-shadow: 0 1px 0 rgba(255, 255, 255, 0.5);
	-webkit-border-radius: 4px;
	-moz-border-radius: 4px;
	border-radius: 4px;
	-webkit-box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.25);
	-moz-box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.25);
	box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.25);
	list-style: none;
	display: none;
	font-size: 12px;
}

#purchaseCurrency {
	font-size: 16px;
	margin-left: 15px;
	color: #19446a;
}

#confirmDetails {
	display: none;
}

.confirmTitle {
	color: #333333;
}


#loadingPaypal {
	display: none;
	text-align: center;
	width: 200px;
	margin: 0 auto;
	font-size: 14px;
}
</style>

<noscript>
	<style>
		#toPurchase {
			display: none;
		}
		
		#confirmDetails {
			display: none;
		}
		
		#loadingPaypal {
			display: none;
		}
		
		#noscript {
			display: block;
		}
	</style>
</noscript>

<?php
	$errorMessage = "";
	
	$posted_email = "";
	$posted_firstName = "";
	$posted_lastName = "";
	$posted_houseNameNumber = "";
	$posted_street = "";
	$posted_town = "";
	$posted_city = "";
	$posted_zipPostCode = "";

	if(isset($_POST["txtEmail"])){
	
		$posted_email = htmlentities($_POST["txtEmail"]);
		$posted_firstName = htmlentities($_POST["txtFirstName"]);
		$posted_lastName = htmlentities($_POST["txtLastName"]);
		$posted_houseNameNumber = htmlentities($_POST["txtHouseNameNumber"]);
		$posted_street = htmlentities($_POST["txtStreet"]);
		$posted_town = htmlentities($_POST["txtTown"]);
		$posted_city = htmlentities($_POST["txtCity"]);
		$posted_zipPostCode = htmlentities($_POST["txtZipPostCode"]);
	
		$requiredArray = array(
			"currency",
			"txtEmail",
			"txtFirstName",
			"txtLastName",
			"txtHouseNameNumber",
			"txtStreet",
			"txtTown",
			"txtCity",
			"country",
			"txtZipPostCode"
		);
		
		$oneEmpty = false;
		
		for($i = 0; $i < count($requiredArray); $i++){
			if(trim($_POST[$requiredArray[$i]]) == ""){
				$oneEmpty = true;
			}
		}
		
		if($oneEmpty == true){
			$errorMessage = "Please fill in all fields";
			?>
			<style>
			#purchaseFormError {
				display: block;
			}
			</style>
			<?php
		} else {
			//CHECK EMAIL
			if(preg_match("/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/", $_POST["txtEmail"])){
				?>
				<style>
					#toPurchase {
						display: none;
					}
					
					#confirmDetails {
						display: block;
					}
				</style>
				<script>
					$(document).ready(function(){
						$(".btnConfirmDetails").click(function(){
							
							$("#confirmDetails").hide();
							$("#loadingPaypal").show();
							
							$("#ipnPost").submit();
						});
					});
				</script>
				
				<form action="https://www.paypal.com/cgi-bin/webscr" method="POST" id="ipnPost">
					<input type="hidden" name="cmd" value="_xclick" />
					<input type="hidden" name="business" value="paypal@moneybug.net" />
					<input type="hidden" name="item_name" value="MoneyBug Guide" />
					<input type="hidden" name="item_number" value="1" />
					<input type="hidden" name="amount" value="<?php echo htmlentities($_POST["price"]); ?>" />
					<input type="hidden" name="no_shipping" value="1" />
					<input type="hidden" name="no_note" value="1" />
					<input type="hidden" name="currency_code" value="<?php echo htmlentities($_POST["currency"]); ?>" />
					<!--<input type="hidden" name="lc" value="GB" />-->
					<input type="hidden" name="return" value="http://www.moneybug.net/payment/thankyou.php" />
					<input type="hidden" name="cancel_return" value="http://www.moneybug.net/payment/cancel.php" />
					<input type="hidden" name="rm" value="2" />
					<input type="hidden" name="notify_url" value="http://www.moneybug.net/payment/ipn.php" />
					
					<?php
					$userDetaisArray = array(
						$posted_email,
						$posted_firstName,
						$posted_lastName,
						$posted_houseNameNumber,
						$posted_street,
						$posted_town,
						$posted_city,
						$_POST["country"],
						$posted_zipPostCode
					);
						
						$customString = "";
						
						foreach($userDetaisArray as $detail){
							$customString .= $detail."|";
						}
					?>
					
					<input type="hidden" name="custom" value="<?php echo $customString; ?>" />
					
				</form>
				<?php
			} else {
				$errorMessage = "Invalid email address";
				?>
				<style>
				#purchaseFormError {
					display: block;
				}
				</style>
				<?php
			}
		}
	}
?>

<script>
	$(document).ready(function(){
		$("#purchaseNow").click(function(){
			
			$("#purchaseFormError").text("");
			$("#purchaseFormError").hide();

			var requiredList = new Array(
				"currency",
				"txtEmail",
				"txtFirstName",
				"txtLastName",
				"txtHouseNameNumber",
				"txtStreet",
				"txtTown",
				"txtCity",
				"country",
				"txtZipPostCode"
			);
			
			var oneEmpty = false;
			
			//CHECK IF A FIELD IS EMPTY
			for(var i = 0; i < requiredList.length; i++){
				if($("[name=" + requiredList[i] + "]").val().trim() == ""){
					oneEmpty = true;
				}
			}
			
			if(oneEmpty == true){
				//All FIELDS MUST BE FILLED IN
				$("#purchaseFormError").text("Please fill in all fields");
				$("#purchaseFormError").show();
				
				$("#purchaseFormError").css({
					"display" : "block"
				});
			} else {
				//CHECK EMAIL
				
				var email = $("[name=txtEmail]").val().trim();
				var emailRegex = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
				var validEmail = emailRegex.test(email);
				
				//CHECK IF FIELD CONTAINS INVALID CHARACTERS
				var characterCheckList = new Array(
					"txtFirstName",
					"txtLastName",
					"txtHouseNameNumber",
					"txtStreet",
					"txtTown",
					"txtCity",
					"txtZipPostCode"
				);
				
				var invalidField = false;
				for(var i = 0; i < characterCheckList.length; i++){
					var regex = /[\<\>!@#\$%^&\*,]+/i;
					
					if(!($("[name=" + characterCheckList[i] + "]").val()).match(/^[a-zA-Z0-9' ]+$/)) {
						invalidField = true;
						
						switch(characterCheckList[i]){
							case "txtFirstName":
								var fieldName = "First name";
							break;
							case "txtLastName":
								var fieldName = "Last name";
							break;
							case "txtHouseNameNumber":
								var fieldName = "House name/number";
								break;
							case "txtStreet":
								var fieldName = "Street";
							break;
								case "txtTown":
								var fieldName = "Town";
							break;
							case "txtCity":
								var fieldName = "City";
							break;
							case "txtZipPostCode":
								var fieldName = "Zip/Post code";
							break;
							
								
						}
						
						$("#purchaseFormError").text(fieldName + " contains invalid character(s)");
						$("#purchaseFormError").show();
						
						$("#purchaseFormError").css({
							"display" : "block"
						});
					}
				}
				
				if(invalidField == false){
					if(validEmail == true){
						$("#FormDetails").submit();
					} else {
						$("#purchaseFormError").text("Invalid email address");
						$("#purchaseFormError").show();
						
						$("#purchaseFormError").css({
							"display" : "block"
						});
					}
				}
		
			}	
	
		});
		
		$("[name=currency]").change(function(){

			
			if($("[name=currency]").val() == "GBP"){
				var price = 9.99;
				var displayPrice = "&pound;9.99";
			} else if($("[name=currency]").val() == "EUR"){
				var price = 11.99;
				var displayPrice = "&euro;11.99";
			} else if($("[name=currency]").val() == "USD") {
				var price = 14.99;
				var displayPrice = "&#36;14.99";
			}
			
			$("#purchaseCurrency").html(displayPrice);
			$("[name=price]").val(price);
		});
	});
</script>
<div id="toPurchase">
   	<div class="header_01">Purchase</div>
        <img src="/images/purchaseCD.png" align="left" style="margin-right:30px;" />
		<p>
		Fill in the form below to purchase your guide. The guide is not electronic and will be delivered to the destination you provide (allow up to 3-7 working days for delivery, depending on location).
		</p>	

	<form action="" method="POST" id="FormDetails">
		<table style="margin:60px auto 0 auto">
			<tr>
				<td colspan="2"><span id="purchaseFormError"><?php echo $errorMessage; ?></span></td>
			</tr>
			<tr>
				<td>Currency: </td><td>
										<select name="currency">
											<option value="USD" selected="selected">USD</option>
											<option value="GBP">GBP</option>
											<option value="EUR">EUR</option>
										</select>
										<span id="purchaseCurrency">$14.99</span>
										<input type="hidden" name="price" value="14.99" />
								   </td>
			</tr>
			<tr>
				<td colspan="2">&nbsp;</td>
			</tr>
			<tr>
				<td>Email: </td><td><input type="text" name="txtEmail" value="<?php echo $posted_email; ?>" maxlength="50" /></td>
			</tr>
			<tr>
				<td>First name: </td><td><input type="text" name="txtFirstName" value="<?php echo $posted_firstName; ?>" maxlength="20" /></td>
			</tr>
			<tr>
				<td>Last name: </td><td><input type="text" name="txtLastName" value="<?php echo $posted_lastName; ?>" maxlength="20" /></td>
			</tr>
			<tr>
				<td colspan="2">&nbsp;</td>
			</tr>
			<tr>
				<td align="center" colspan="2"><h3>Deliver to:</h3></td>
			</tr>
			<tr>
				<td>House name/number: </td><td><input type="text" name="txtHouseNameNumber" value="<?php echo $posted_houseNameNumber; ?>" maxlength="20" /></td>
			</tr>
			<tr>
				<td>Street: </td><td><input type="text" name="txtStreet" value="<?php echo $posted_street; ?>" maxlength="30" /></td>
			</tr>
			<tr>
				<td>Town: </td><td><input type="text" name="txtTown" value="<?php echo $posted_town; ?>" maxlength="30" /></td>
			</tr>
			<tr>
				<td>City: </td><td><input type="text" name="txtCity" value="<?php echo $posted_city; ?>" maxlength="30" /></td>
			</tr>
			<tr>
				<td>Zip/Post code: </td><td><input type="text" name="txtZipPostCode" value="<?php echo $posted_zipPostCode; ?>" maxlength="20" /></td>
			</tr>
			<tr>
				<td>Country: </td><td><?php include "php/countryList.php"; ?></td>
			</tr>
			<tr>
				<td colspan="2">&nbsp;</td>
			</tr>
			<tr>
				<td colspan="2">If this form does not take the address you want, please contact us.</td>
			</tr>
			<tr>
				<td colspan="2">&nbsp;</td>
			</tr>
			<tr>
				<td colspan="2">
					<a href="javascript:void(0);" id="purchaseNow">Proceed</a>
				</td>
			</tr>
		</table>
	</form>
</div>

<div id="noscript">
	To view this page you need to enable Javascript. Click <a href="http://support.google.com/bin/answer.py?hl=en&answer=23852" target="_blank">HERE</a> to find out how to enable Javascript.
</div>

<div id="confirmDetails">
	<div class="header_01">Confirm Details</div>
	<table style="margin: 0 auto;">
		<tr>
			<td class='confirmTitle'>Cost: </td><td><?php echo @htmlentities($_POST["price"]); ?></td>
		</tr>
		<tr>
			<td class='confirmTitle'>Currency: </td><td><?php echo @htmlentities($_POST["currency"]); ?></td>
		</tr>
		<tr>
			<td colspan="2">&nbsp;</td>
		</tr>
		<tr>
			<td class='confirmTitle'>Email: </td><td><?php echo @$posted_email; ?></td>
		</tr>
		<tr>
			<td class='confirmTitle'>First name: </td><td><?php echo @ucfirst($posted_firstName); ?></td>
		</tr>
		<tr>
			<td class='confirmTitle'>Last name: </td><td><?php echo @ucfirst($posted_lastName); ?></td>
		</tr>
		<tr>
			<td colspan="2">&nbsp;</td>
		</tr>
		<tr>
			<td class='confirmTitle'>House name/number: </td><td><?php echo @ucwords($posted_houseNameNumber); ?></td>
		</tr>
		<tr>
			<td class='confirmTitle'>Street: </td><td><?php echo @ucwords($posted_street); ?></td>
		</tr>
		<tr>
			<td class='confirmTitle'>Town: </td><td><?php echo @ucwords($posted_town); ?></td>
		</tr>
		<tr>
			<td class='confirmTitle'>City: </td><td><?php echo @ucwords($posted_city); ?></td>
		</tr>
		<tr>
			<td class='confirmTitle'>Zip/Post code: </td><td><?php echo @strtoupper($posted_zipPostCode); ?></td>
		</tr>
		<tr>
			<td class='confirmTitle'>Country: </td><td><?php echo @htmlentities($_POST["country"]); ?></td>
		</tr>
		<tr>
			<td colspan="2">&nbsp;</td>
		</tr>
		<tr>
			<td colspan="2" style="border-top:1px solid #E2E2E2;padding-top:15px;">By purchasing this guide you accept the <a href="/terms" target="_blank">terms & conditions</a>. You will now be redirected to Paypal to pay for this guide. Please note a paypal account is not required.</td>
		</tr>
		<tr>
			<td colspan="2">&nbsp;</td>
		</tr>
		
		<tr>
			<td colspan="2">
				<a href="javascript:void(0);" id="purchaseNow" class="btnConfirmDetails">Confirm</a>
			</td>
		</tr>
	</table>
</div>

<div id="loadingPaypal">
	<p>Redirecting to Paypal...</p>
	<img src="/images/loading.gif" />
</div>
			
<?php require "inc/pageBottom.php"; ?>   